The goals / steps of this project are the following:
# Importing some useful packages
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
import pickle
%matplotlib inline
# Helper functions
# Show 2 images on the same line for easy comparison
def show2img(img1, img2, title1, title2):
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()
ax1.imshow(img1)
ax1.set_title(title1, fontsize=50)
ax2.imshow(img2)
ax2.set_title(title2, fontsize=50)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
# Show 2 images (the second one with gray scale) on the same line for easy comparison
def show2img_gray(img1, img2, title1, title2):
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()
ax1.imshow(img1)
ax1.set_title(title1, fontsize=50)
ax2.imshow(img2, cmap='gray')
ax2.set_title(title2, fontsize=50)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
# Show 3 images on the same line for easy comparison
def show3img(img1, img2, img3, title1, title2, title3):
f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 6))
f.tight_layout()
ax1.imshow(img1)
ax1.set_title(title1, fontsize=50)
ax2.imshow(img2)
ax2.set_title(title2, fontsize=50)
ax3.imshow(img3)
ax3.set_title(title3, fontsize=50)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
# Prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
nx = 9 # the number of inside corners in x
ny = 6 # the number of inside corners in y
objp = np.zeros((ny*nx,3), np.float32)
objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
# Make a list of calibration images
images = os.listdir("camera_cal/")
# Step through the list and search for chessboard corners
for fname in images:
# Read image
img = cv2.imread('camera_cal/'+fname)
# Create grayscale image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (nx,ny),None)
if ret == True:
# If found, add object points, image points
objpoints.append(objp)
imgpoints.append(corners)
# Draw and display the corners
img = cv2.drawChessboardCorners(img, (nx,ny), corners, ret)
write_name = 'output_images/corners_'+fname
cv2.imwrite(write_name, img)
else:
# If not, return the filename
print(fname,"can't be processed")
# Show an image (calibration2.jpg) & its chessboard corners
img1 = cv2.imread('camera_cal/calibration2.jpg')
img2 = cv2.imread('output_images/corners_calibration2.jpg')
show2img(img1, img2, 'Calibration image', 'Image with chessboard corners')
# Apply camera calibration to get the matrix (mtx) and distortion coefficients (dist)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# Print out the value of mxt & dist
print("Here is the matrix (mtx):")
print(mtx)
print("Here is the distortion coefficients (dist):")
print(dist)
# Save the camera calibration result for later use
dist_pickle = {}
dist_pickle["mtx"] = mtx
dist_pickle["dist"] = dist
pickle.dump(dist_pickle, open("calibrated_result.p","wb"))
# Read the matrix (mtx) and coefficients (dist) from calibrated result
# Use the saved data to avoid running the calibration from the beginning when restarting kernel, etc
dist_pickle = pickle.load(open("calibrated_result.p", "rb"))
mtx = dist_pickle["mtx"]
dist = dist_pickle["dist"]
# Undistort each chessboard image using the calculated matrix (mtx) & coefficients (dist)
for fname in images:
# Read image
img = cv2.imread('camera_cal/'+fname)
# Create undistorted image & save to file in "output_images" folder
undist = cv2.undistort(img, mtx, dist, None, mtx)
write_name = 'output_images/undistorted_'+fname
cv2.imwrite(write_name, undist)
# Show 3 images: original, with chessboard corners, undistorted
img1 = cv2.imread('camera_cal/calibration3.jpg')
img2 = cv2.imread('output_images/corners_calibration3.jpg')
img3 = cv2.imread('output_images/undistorted_calibration3.jpg')
show3img(img1, img2, img3, 'Original', 'With corners', 'Undistorted')
# Generate undistorted image and show with its raw image
raw_original = mpimg.imread('test_images/test1.jpg')
raw_undist = cv2.undistort(raw_original, mtx, dist, None, mtx)
show2img(raw_original, raw_undist, 'Original image', 'Undistorted image')
"""
For writeup:
- Apply the process to undistort all images in "/test_images/" folder
- Save the undistorted images to "/output_images/" folder
"""
# A function to undistort all images in the list
def undistort_all(list):
# Undistort each test image
for fname in list:
# Read image
img = cv2.imread('test_images/'+fname)
# Create undistorted image & save to file in "output_images" folder
undist = cv2.undistort(img, mtx, dist, None, mtx)
write_name = 'output_images/undistorted_'+fname
cv2.imwrite(write_name, undist)
# List of test images
test_images = os.listdir("test_images/")
# Call the procees to undistort all images
undistort_all(test_images)
# A function that takes an image, gradient orientation,
# and threshold min / max values.
def abs_sobel_thresh(img, orient='x', sobel_kernel=3, thresh=(0, 255)):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Apply x or y gradient with the OpenCV Sobel() function
# and take the absolute value
if orient == 'x':
abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
if orient == 'y':
abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
# Rescale back to 8 bit integer
scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
# Create a copy and apply the threshold to generate binary image
grad_binary = np.zeros_like(scaled_sobel)
grad_binary[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1
return grad_binary
# A function to return the magnitude of the gradient
# for a given sobel kernel size and threshold values
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Take both Sobel x and y gradients
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
# Calculate the gradient magnitude
gradmag = np.sqrt(sobelx**2 + sobely**2)
# Rescale to 8 bit
scale_factor = np.max(gradmag)/255
gradmag = (gradmag/scale_factor).astype(np.uint8)
# Create a binary image of ones where threshold is met, zeros otherwise
mag_binary = np.zeros_like(gradmag)
mag_binary[(gradmag >= mag_thresh[0]) & (gradmag <= mag_thresh[1])] = 1
return mag_binary
# A function to return the direction of the gradient
# for a given sobel kernel size and threshold values
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):
# Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Calculate the x and y gradients
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
# Take the absolute value of the gradient direction,
# apply a threshold, and create a binary image result
absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx))
dir_binary = np.zeros_like(absgraddir)
dir_binary[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1
return dir_binary
# A function to threshold an image for a given range of color
def color_thresh(img, color_thresh):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
color_binary = np.zeros_like(gray)
color_binary[(gray > color_thresh[0]) & (gray <= color_thresh[1])] = 1
return color_binary
# A function that thresholds the S-channel of HLS
def hls_select(img, thresh=(0, 255)):
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
s_channel = hls[:,:,2]
hls_binary = np.zeros_like(s_channel)
hls_binary[(s_channel > thresh[0]) & (s_channel <= thresh[1])] = 1
return hls_binary
# A function to combine several thresholds
def combined_thresh(img):
# Generate the binary image for each threshold
gradx = abs_sobel_thresh(img, orient='x', thresh=(20,100))
grady = abs_sobel_thresh(img, orient='y', thresh=(20,100))
mag_binary = mag_thresh(img, sobel_kernel=3, mag_thresh=(30, 100))
dir_binary = dir_threshold(img, sobel_kernel=15, thresh=(0.7, 1.3))
# Binary image with combined thresholds
combined_binary = np.zeros_like(dir_binary)
combined_binary[((gradx == 1) & (grady == 1)) | ((mag_binary == 1) & (dir_binary == 1))] = 1
return combined_binary
# Select default thresholding method
# 1 : Color threshold
# 2 : HLS S-Channel threshold
# 3 : Combined threshold
threshold_method = 1
# A function to select method for applying thresholds
def thresh_binary(img):
if threshold_method == 1:
binary_image = color_thresh(img, color_thresh=(200, 255))
elif threshold_method == 2:
binary_image = hls_select(img, thresh=(180, 255))
elif threshold_method == 3:
binary_image = combined_thresh(img)
return binary_image
# Read the image to be tested
test_img = mpimg.imread('test_images/test1.jpg')
# Undistort the test image
test_img_undist = cv2.undistort(test_img, mtx, dist, None, mtx)
# Call the function to apply gradient threshold (on x direction)
gradx = abs_sobel_thresh(test_img_undist, orient='x', thresh=(20,100))
# Show 2 images (original & binary)
show2img_gray(test_img_undist, gradx, "Original Image", "Gradient Threshold (x axis)")
# Call the function to apply gradient threshold (on y direction)
grady = abs_sobel_thresh(test_img_undist, orient='y', thresh=(20,100))
# Show 2 images (original & binary)
show2img_gray(test_img_undist, grady, "Original Image", "Gradient Threshold(y axis)")
# Call the function to apply gradient magnitude threshold
mag_binary = mag_thresh(test_img_undist, sobel_kernel=3, mag_thresh=(30, 100))
# Show 2 images (original & binary)
show2img_gray(test_img_undist, mag_binary, "Original Image", "Magnitude Threshold")
# Call the function to apply gradient direction threshold
dir_binary = dir_threshold(test_img_undist, sobel_kernel=15, thresh=(0.7, 1.3))
# Show 2 images (original & binary)
show2img_gray(test_img_undist, dir_binary, "Original Image", "Direction Threshold")
# Binary image with combined thresholds
combined_binary = combined_thresh(test_img_undist)
# Show 2 images (original & binary)
show2img_gray(test_img_undist, combined_binary, "Original Image", "Combined thresholds")
# Binary image with color threshold
color_binary = color_thresh(test_img_undist, color_thresh=(200, 255))
# Show 2 images (original & binary)
show2img_gray(test_img_undist, color_binary, "Original Image", "Color Threshold")
# Binary image with color threshold
hls_binary = hls_select(test_img_undist, thresh=(180, 255))
# Show 2 images (original & binary)
show2img_gray(test_img_undist, hls_binary, "Original Image", "S-Channel (HLS) Threshold")
"""
- Apply the process to all test images folder to create thresholded binary images
- Save the thresholded binary images to "/output_binary_images/" folder for later use
"""
# A function to create thresholded binary image from all images in the list
def threshold_all(list):
for fname in list:
# Read test image
test_img = mpimg.imread('test_images/'+fname)
# Undistort the test image
test_img_undist = cv2.undistort(test_img, mtx, dist, None, mtx)
# Call the function to apply gradient threshold (on x direction)
gradx = abs_sobel_thresh(test_img_undist, orient='x', thresh=(20,100))
# Call the function to apply gradient threshold (on y direction)
grady = abs_sobel_thresh(test_img_undist, orient='y', thresh=(20,100))
# Call the function to apply gradient magnitude threshold
mag_binary = mag_thresh(test_img_undist, sobel_kernel=3, mag_thresh=(30, 100))
# Call the function to apply gradient direction threshold
dir_binary = dir_threshold(test_img_undist, sobel_kernel=15, thresh=(0.7, 1.3))
# Binary image with color threshold
color_binary = color_thresh(test_img_undist, color_thresh=(200, 255))
# Binary image with color threshold
hls_binary = hls_select(test_img_undist, thresh=(180, 255))
# Binary image with combined thresholds
combined_binary = combined_thresh(test_img_undist)
# Save the thresholded binary images
mpimg.imsave('output_binary_images/gradx_'+fname, gradx, cmap="gray")
mpimg.imsave('output_binary_images/grady_'+fname, grady, cmap="gray")
mpimg.imsave('output_binary_images/mag_'+fname, mag_binary, cmap="gray")
mpimg.imsave('output_binary_images/dir_'+fname, dir_binary, cmap="gray")
mpimg.imsave('output_binary_images/color_'+fname, color_binary, cmap="gray")
mpimg.imsave('output_binary_images/hls_'+fname, hls_binary, cmap="gray")
mpimg.imsave('output_binary_images/combined_'+fname, combined_binary, cmap="gray")
# List of test images
test_images = os.listdir("test_images/")
# Call the process to create thresholded binary images from all test images
threshold_all(test_images)
# A function to perform perspective transform to image from given points (src & dst)
def warped(img, src, dst):
# Get image size
img_size = (img.shape[1], img.shape[0])
# Calculate transform matrix
M = cv2.getPerspectiveTransform(src, dst)
# Apply perpective transform with same size as input image
warped = cv2.warpPerspective(img, M, img_size, flags=cv2.INTER_NEAREST)
return warped, M
# A function to draw lines connecting 4 points
def draw_boundary(img, fourpoints, color, thickness):
p1 = np.int32(fourpoints)[0]
p2 = np.int32(fourpoints)[1]
p3 = np.int32(fourpoints)[2]
p4 = np.int32(fourpoints)[3]
cv2.line(img, (p1[0], p1[1]), (p2[0], p2[1]), color, thickness)
cv2.line(img, (p2[0], p2[1]), (p3[0], p3[1]), color, thickness)
cv2.line(img, (p3[0], p3[1]), (p4[0], p4[1]), color, thickness)
cv2.line(img, (p4[0], p4[1]), (p1[0], p1[1]), color, thickness)
return img
# Choose reference points for the transform (src & dst)
# Note: currently picking up from writeup template. To be optimized later.
img_size = (test_img.shape[1], test_img.shape[0])
src = np.float32(
[[(img_size[0] / 2) - 55, img_size[1] / 2 + 100],
[((img_size[0] / 6) - 10), img_size[1]],
[(img_size[0] * 5 / 6) + 60, img_size[1]],
[(img_size[0] / 2 + 55), img_size[1] / 2 + 100]])
dst = np.float32(
[[(img_size[0] / 4), 0],
[(img_size[0] / 4), img_size[1]],
[(img_size[0] * 3 / 4), img_size[1]],
[(img_size[0] * 3 / 4), 0]])
# Read the test image with straight line
straight_img = mpimg.imread('test_images/straight_lines1.jpg')
# Undistort the test image
straight_img_undist = cv2.undistort(straight_img, mtx, dist, None, mtx)
# Create a warped image by perspective transform
straight_img_warped, M_perspective = warped(straight_img_undist, src, dst)
# Draw the boundary lines connecting 4 points of src & dst
straight_img_undist_lines = draw_boundary(straight_img_undist, src, color=(255,0,0), thickness=4)
straight_img_warped_lines = draw_boundary(straight_img_warped, dst, color=(255,0,0), thickness=4)
# Show 2 images
show2img(straight_img_undist_lines, straight_img_warped_lines, "Undistorted Image", "Warped Image")
# Save 2 images
mpimg.imsave('output_images/straight_img_undist_lines.jpg', straight_img_undist_lines)
mpimg.imsave('output_images/straight_img_warped_lines.jpg', straight_img_warped_lines)
# Create a warped image by perspective transform
test_img_warped, M_perspective = warped(test_img_undist, src, dst)
# Show 2 images
show2img(test_img_undist, test_img_warped, "Undistorted Image", "Warped Image")
# Create a thresholded binary image with selected method (for further testing)
thresh_img = thresh_binary(test_img_undist)
# Create a warped binary image by perspective transform
thresh_img_warped, M_perspective = warped(thresh_img, src, dst)
# Show 2 images
show2img_gray(test_img_warped, thresh_img_warped, "Warped Image", "Warped Binary Image")
# A function to run perspective transform to all images in the list
def transform_all(list):
for fname in list:
# Read image
test_img = mpimg.imread('test_images/'+fname)
# Undistort the test image
test_img_undist = cv2.undistort(test_img, mtx, dist, None, mtx)
# Create a warped image by perspective transform
test_img_warped, M_perspective = warped(test_img_undist, src, dst)
# Save warped image
mpimg.imsave('output_images/warped_'+fname, test_img_warped)
# Apply perspective transform to all test images & save to files (for writeup)
transform_all(test_images)
# A function to create histogram of given image
def hist(img):
# Grab only the bottom half of the image
# Lane lines are likely to be mostly vertical nearest to the car
bottom_half = img[img.shape[0]//2:,:]
# Sum across image pixels vertically - make sure to set an `axis`
# i.e. the highest areas of vertical lines should be larger values
histogram = np.sum(bottom_half, axis=0)
return histogram
# Read the binary image to be called for histogram creation
# Specify here a binary image which is thresholded & transformed above
binaryimage = thresh_img_warped
# Create histogram of image binary activations
histogram = hist(binaryimage)
# Visualize the resulting histogram
plt.plot(histogram)
# A function to find lane pixels from binary warped image
def find_lane_pixels(binary_warped):
# Take a histogram of the bottom half of the image
histogram = np.sum(binary_warped[binary_warped.shape[0]//2:,:], axis=0)
# Create an output image to draw on and visualize the result
out_img = np.dstack((binary_warped, binary_warped, binary_warped))
# Find the peak of the left and right halves of the histogram
# These will be the starting point for the left and right lines
midpoint = np.int(histogram.shape[0]//2)
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint
# HYPERPARAMETERS
# Choose the number of sliding windows
nwindows = 9
# Set the width of the windows +/- margin
margin = 100
# Set minimum number of pixels found to recenter window
minpix = 50
# Set height of windows - based on nwindows above and image shape
window_height = np.int(binary_warped.shape[0]//nwindows)
# Identify the x and y positions of all nonzero pixels in the image
nonzero = binary_warped.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
# Current positions to be updated later for each window in nwindows
leftx_current = leftx_base
rightx_current = rightx_base
# Create empty lists to receive left and right lane pixel indices
left_lane_inds = []
right_lane_inds = []
# Step through the windows one by one
for window in range(nwindows):
# Identify window boundaries in x and y (and right and left)
win_y_low = binary_warped.shape[0] - (window+1)*window_height
win_y_high = binary_warped.shape[0] - window*window_height
win_xleft_low = leftx_current - margin
win_xleft_high = leftx_current + margin
win_xright_low = rightx_current - margin
win_xright_high = rightx_current + margin
# Draw the windows on the visualization image
cv2.rectangle(out_img,(win_xleft_low,win_y_low),
(win_xleft_high,win_y_high),(0,255,0), 2)
cv2.rectangle(out_img,(win_xright_low,win_y_low),
(win_xright_high,win_y_high),(0,255,0), 2)
# Identify the nonzero pixels in x and y within the window #
good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0]
good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0]
# Append these indices to the lists
left_lane_inds.append(good_left_inds)
right_lane_inds.append(good_right_inds)
# If the number of detected pixels exceed threshold (minpix),
# recenter next window on the mean position
if len(good_left_inds) > minpix:
leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
if len(good_right_inds) > minpix:
rightx_current = np.int(np.mean(nonzerox[good_right_inds]))
# Concatenate the arrays of indices (previously was a list of lists of pixels)
try:
left_lane_inds = np.concatenate(left_lane_inds)
right_lane_inds = np.concatenate(right_lane_inds)
except ValueError:
# Avoids an error if the above is not implemented fully
pass
# Extract left and right line pixel positions
leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
return leftx, lefty, rightx, righty, left_lane_inds, right_lane_inds, out_img
# A function to fit a polynomial from detected lane pixels
def fit_polynomial(binary_warped):
# Find our lane pixels first
leftx, lefty, rightx, righty, left_lane_inds, right_lane_inds, out_img = find_lane_pixels(binary_warped)
# Fit a second order polynomial to each using `np.polyfit`
left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)
# Generate x and y values for plotting
ploty = np.linspace(0, binary_warped.shape[0]-1, binary_warped.shape[0] )
try:
left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]
except TypeError:
# Avoids an error if `left` and `right_fit` are still none or incorrect
print('The function failed to fit a line!')
left_fitx = 1*ploty**2 + 1*ploty
right_fitx = 1*ploty**2 + 1*ploty
## Visualization ##
# Colors in the left and right lane regions
out_img[lefty, leftx] = [255, 0, 0]
out_img[righty, rightx] = [0, 0, 255]
# Plots the left and right polynomials on the lane lines
plt.plot(left_fitx, ploty, color='yellow')
plt.plot(right_fitx, ploty, color='yellow')
return out_img
# Apply the process (to find lane pixels & fit polynomial) to binary image
out_img = fit_polynomial(thresh_img_warped)
# Show the output image
plt.imshow(out_img)
# A function to fit the second order polynomial
def fit_poly(img_shape, leftx, lefty, rightx, righty):
# Fit a second order polynomial to each with np.polyfit()
if leftx.any() and lefty.any():
left_fit = np.polyfit(lefty, leftx, 2)
else:
# If no lane pixels are found (rarely happen), use the default values
# This default value is tuned & taken from a test image (temporary value)
left_fit = np.array([-5.82876789e-05, 9.89023579e-03, 3.63726419e+02])
if rightx.any() and rightx.any():
right_fit = np.polyfit(righty, rightx, 2)
else:
# If no lane pixels are found (rarely happen), use the default values
# This default value is tuned & taken from a test image (temporary value)
right_fit = np.array([1.13367624e-04, -3.17551122e-01, 1.14925867e+03])
# Save the polynomial fit parameters
lastFrame_save(lane_pickle, "lane_params.p", left_fit, right_fit)
# Generate x and y values for plotting
ploty = np.linspace(0, img_shape[0]-1, img_shape[0])
# Calc both polynomials using ploty, left_fit and right_fit
left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]
return left_fitx, right_fitx, ploty
# A function to search around the polynomial
def search_around_poly(binary_warped):
# HYPERPARAMETER
# Choose the width of the margin around the previous polynomial to search
margin = 100
# Grab activated pixels
nonzero = binary_warped.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
if search_method == 1:
# Find our lane pixels first (for the first frame)
leftx, lefty, rightx, righty, left_lane_inds, right_lane_inds, out_img_tmp = find_lane_pixels(binary_warped)
# Fit new polynomials
left_fitx, right_fitx, ploty = fit_poly(binary_warped.shape, leftx, lefty, rightx, righty)
elif search_method == 2:
# Load the saved parameters of last frame
left_fit, right_fit = lastFrame_load("lane_params.p")
# If no data is saved, create it
if left_fit.any() == False or right_fit.any() == False:
# Find lane pixels (for the first frame)
leftx, lefty, rightx, righty, eft_lane_inds, right_lane_inds, out_img_tmp = find_lane_pixels(binary_warped)
# Calculate & save polynomial fit parameters for left & right lane
left_fitx_tmp, right_fitx_tmp, ploty_tmp = fit_poly(binary_warped.shape, leftx, lefty, rightx, righty)
# Load the saved parameters of last frame
left_fit, right_fit = lastFrame_load("lane_params.p")
# Set the area of search based on activated x-values
# within the +/- margin of our polynomial function
left_lane_inds = ((nonzerox > (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy +
left_fit[2] - margin)) & (nonzerox < (left_fit[0]*(nonzeroy**2) +
left_fit[1]*nonzeroy + left_fit[2] + margin)))
right_lane_inds = ((nonzerox > (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy +
right_fit[2] - margin)) & (nonzerox < (right_fit[0]*(nonzeroy**2) +
right_fit[1]*nonzeroy + right_fit[2] + margin)))
# Again, extract left and right line pixel positions
leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
# Fit new polynomials
left_fitx, right_fitx, ploty = fit_poly(binary_warped.shape, leftx, lefty, rightx, righty)
# Temporary fix
left_ave = np.average(left_fitx)
right_ave = np.average(right_fitx)
diff_ave = right_ave - left_ave
if (diff_ave < 400) or (left_ave > 1280) or (right_ave > 1280):
# The detected lane lines are not suitable. Need to detect by other way
# Find lane pixels
leftx, lefty, rightx, righty, left_lane_inds, right_lane_inds, out_img_tmp = find_lane_pixels(binary_warped)
# Calculate & save polynomial fit parameters for left & right lane
left_fitx, right_fitx, ploty = fit_poly(binary_warped.shape, leftx, lefty, rightx, righty)
# Change margin to smaller value to display the lane lines
# Still keep big margin when searching for high accuracy
margin = 30
## Visualization ##
# Create an image to draw on and an image to show the selection window
out_img = np.dstack((binary_warped, binary_warped, binary_warped))*255
window_img = np.zeros_like(out_img)
# Color in left and right line pixels
out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [0,0,255]
out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0,0,255]
# Generate a polygon to illustrate the search window area
# And recast the x and y points into usable format for cv2.fillPoly()
left_line_window1 = np.array([np.transpose(np.vstack([left_fitx-margin, ploty]))])
left_line_window2 = np.array([np.flipud(np.transpose(np.vstack([left_fitx+margin,
ploty])))])
left_line_pts = np.hstack((left_line_window1, left_line_window2))
right_line_window1 = np.array([np.transpose(np.vstack([right_fitx-margin, ploty]))])
right_line_window2 = np.array([np.flipud(np.transpose(np.vstack([right_fitx+margin,
ploty])))])
right_line_pts = np.hstack((right_line_window1, right_line_window2))
# Define the middle area between detected lane lines
middle_area = np.hstack((left_line_window2, right_line_window1))
# Fill middle area with color Green
cv2.fillPoly(window_img, np.int_([middle_area]), (0, 255, 0))
# Draw the lane onto the warped blank image
cv2.fillPoly(window_img, np.int_([left_line_pts]), (255,0,0))
cv2.fillPoly(window_img, np.int_([right_line_pts]), (255,0,0))
result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)
# Plot the polynomial lines onto the image
plt.plot(left_fitx, ploty, color='yellow')
plt.plot(right_fitx, ploty, color='yellow')
## End visualization steps ##
return result, ploty, left_fitx, right_fitx
# Pickle to keep lane line parameters of last frame
lane_pickle = {}
# A function to initialize the parameters of last frame to empty array
# Parameters : coefficients for left & right lane
def lastFrame_init(lane_pickle, filepath):
lane_pickle["left_fit"] = np.array([])
lane_pickle["right_fit"] = np.array([])
pickle.dump(lane_pickle, open(filepath,"wb"))
# A function to save the parameters of last frame
# Parameters : coefficients for left & right lane
def lastFrame_save(lane_pickle, filepath, left_fit, right_fit):
lane_pickle["left_fit"] = left_fit
lane_pickle["right_fit"] = right_fit
pickle.dump(lane_pickle, open(filepath,"wb"))
# A function to load the parameters of last frame
# Parameters : coefficients for left & right lane
def lastFrame_load(filepath):
lane_pickle = pickle.load(open(filepath,"rb"))
left_fit = lane_pickle["left_fit"]
right_fit = lane_pickle["right_fit"]
return left_fit, right_fit
# Select the search method before processing the pipeline
# 1 : process each frame separately to find lane pixels & fit a polynomial
# 2 : use the parameters from last frame to search lane pixels & fit a polynomial
# 3 : use convolution search (not fully supported for processing video yet)
search_method = 2
# Polynomial fit values from the previous frame
# Grab the actual values from the previous step
# Just reference data. Will be optimized later
left_fit_ref = np.array([-5.82876789e-05, 9.89023579e-03, 3.63726419e+02])
right_fit_ref = np.array([1.13367624e-04, -3.17551122e-01, 1.14925867e+03])
# Set the reference parameters before running the pipeline
lastFrame_save(lane_pickle, "lane_params.p", left_fit_ref, right_fit_ref)
# Apply the search pipepline to binary image
boundary, ploty, left_fitx, right_fitx = search_around_poly(thresh_img_warped)
# View output image
plt.imshow(boundary)
# A function to create the mask window
def window_mask(width, height, img_ref, center,level):
output = np.zeros_like(img_ref)
output[int(img_ref.shape[0]-(level+1)*height):int(img_ref.shape[0]-level*height),max(0,int(center-width/2)):min(int(center+width/2),img_ref.shape[1])] = 1
return output
# A function to find window centroids
def find_window_centroids(image, window_width, window_height, margin):
window_centroids = [] # Store the (left,right) window centroid positions per level
window = np.ones(window_width) # Create our window template that we will use for convolutions
# First find the two starting positions for the left and right lane by using np.sum to get the vertical image slice
# and then np.convolve the vertical image slice with the window template
# Sum quarter bottom of image to get slice, could use a different ratio
l_sum = np.sum(image[int(3*image.shape[0]/4):,:int(image.shape[1]/2)], axis=0)
l_center = np.argmax(np.convolve(window,l_sum))-window_width/2
r_sum = np.sum(image[int(3*image.shape[0]/4):,int(image.shape[1]/2):], axis=0)
r_center = np.argmax(np.convolve(window,r_sum))-window_width/2+int(image.shape[1]/2)
# Add what we found for the first layer
window_centroids.append((l_center,r_center))
# Go through each layer looking for max pixel locations
for level in range(1,(int)(image.shape[0]/window_height)):
# convolve the window into the vertical slice of the image
image_layer = np.sum(image[int(image.shape[0]-(level+1)*window_height):int(image.shape[0]-level*window_height),:], axis=0)
conv_signal = np.convolve(window, image_layer)
# Find the best left centroid by using past left center as a reference
# Use window_width/2 as offset because convolution signal reference is at right side of window, not center of window
offset = window_width/2
l_min_index = int(max(l_center+offset-margin,0))
l_max_index = int(min(l_center+offset+margin,image.shape[1]))
l_center = np.argmax(conv_signal[l_min_index:l_max_index])+l_min_index-offset
# Find the best right centroid by using past right center as a reference
r_min_index = int(max(r_center+offset-margin,0))
r_max_index = int(min(r_center+offset+margin,image.shape[1]))
r_center = np.argmax(conv_signal[r_min_index:r_max_index])+r_min_index-offset
# Add what we found for that layer
window_centroids.append((l_center,r_center))
return window_centroids
# A function to search sliding window by using convolution
def convolution_search(img):
# Read in a thresholded image
warped = img
# window settings
window_width = 50
window_height = 80 # Break image into 9 vertical layers since image height is 720
margin = 100 # How much to slide left and right for searching
window_centroids = find_window_centroids(warped, window_width, window_height, margin)
# If we found any window centers
if len(window_centroids) > 0:
# Points used to draw all the left and right windows
l_points = np.zeros_like(warped)
r_points = np.zeros_like(warped)
# Go through each level and draw the windows
for level in range(0,len(window_centroids)):
# Window_mask is a function to draw window areas
l_mask = window_mask(window_width,window_height,warped,window_centroids[level][0],level)
r_mask = window_mask(window_width,window_height,warped,window_centroids[level][1],level)
# Add graphic points from window mask here to total pixels found
l_points[(l_points == 255) | ((l_mask == 1) ) ] = 255
r_points[(r_points == 255) | ((r_mask == 1) ) ] = 255
# Draw the results
template = np.array(r_points+l_points,np.uint8) # add both left and right window pixels together
zero_channel = np.zeros_like(template) # create a zero color channel
template = np.array(cv2.merge((zero_channel,template,zero_channel)),np.uint8) # make window pixels green
warpage= np.dstack((warped, warped, warped))*255 # making the original road pixels 3 color channels
output = cv2.addWeighted(warpage, 1, template, 0.5, 0.0) # overlay the orignal road image with window results
# If no window centers found, just display orginal road image
else:
output = np.array(cv2.merge((warped,warped,warped)),np.uint8)
return output
# Apply the search pipepline to binary image
boundary_window = convolution_search(thresh_img_warped)
# View output image
plt.imshow(boundary_window)
# A helper function to combine the generated binary image with initial image
def weighted_img(img, initial_img, α=0.8, β=1., γ=0.):
"""
`img`: output image of the process
`initial_img`: the image before any processing.
The result image is computed as follows:
initial_img * α + img * β + γ
NOTE: initial_img and img must be the same shape!
"""
return cv2.addWeighted(initial_img, α, img, β, γ)
# Unwarped the binary image
# Apply perspective transform reversely by switching src & dst
unwarped, M_perspective_reverse = warped(boundary, dst, src)
# Combine unwarped binary image with boundaries to initial image
combined = weighted_img(test_img_undist, unwarped)
# Show unwarped & combined image
show2img(unwarped,combined, "Unwarped image with boundaries", "Combined with original image")
# Unwarped the binary image
# Apply perspective transform reversely by switching src & dst
unwarped_conv, M_perspective_reverse = warped(boundary_window, dst, src)
# Combine unwarped binary image with boundaries to initial image
combined_conv = weighted_img(test_img_undist, unwarped_conv)
# Show unwarped & combined image
show2img(unwarped_conv,combined_conv, "Unwarped image with boundaries", "Combined with original image")
# A function to add culvature radius and distance from center to image
def add_radius_distance(img, radius, distance):
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, "Radius of curvature = %.0fm" % radius, (50, 50), font, 2, (255,0,0), thickness=2)
if distance > 0:
cv2.putText(img, "Vehicle is %.2fm right of center" % distance, (50, 120), font, 2, (255,0,0), thickness=2)
else:
cv2.putText(img, "Vehicle is %.2fm left of center" % np.abs(distance), (50, 120), font, 2, (255,0,0), thickness=2)
# A function to calculate culvature radius and relative position from lane coordinates
def measure_curvature_radius_position(ploty, left_fitx, right_fitx):
'''
Calculates the curvature of polynomial functions and distance from center in meters.
'''
# Define conversions in x and y from pixels space to meters
ym_per_pix = 30/720 # meters per pixel in y dimension
xm_per_pix = 3.7/700 # meters per pixel in x dimension
# Fit a second order polynomial to pixel positions in each fake lane line
# Fit new polynomials to x,y in world space
left_fit_cr = np.polyfit(ploty*ym_per_pix, left_fitx*xm_per_pix, 2)
right_fit_cr = np.polyfit(ploty*ym_per_pix, right_fitx*xm_per_pix, 2)
# Define y-value where we want radius of curvature
# Choose the maximum y-value, corresponding to the bottom of the image
y_eval = np.max(ploty) * ym_per_pix
# Calculation of R_curve (radius of curvature)
left_curverad = ((1 + (2*left_fit_cr[0]*y_eval + left_fit_cr[1])**2)**1.5) / np.absolute(2*left_fit_cr[0])
right_curverad = ((1 + (2*right_fit_cr[0]*y_eval + right_fit_cr[1])**2)**1.5) / np.absolute(2*right_fit_cr[0])
radius = (left_curverad + right_curverad)/2
# Calculate the position of left line & right lane nearest to the camera
left_pos = left_fit_cr[0]*(y_eval**2) + left_fit_cr[1]*y_eval + left_fit_cr[2]
right_pos = right_fit_cr[0]*(y_eval**2) + right_fit_cr[1]*y_eval + right_fit_cr[2]
# Position of the center of the 2 lanes in the image
position = (left_pos + right_pos)/2
# Distance from the center of the image
# Assuming here that image.shape[1] = 1280
distance_from_center = position - (1280/2)*xm_per_pix
return radius, distance_from_center
# Calculate curvature radius & distance from center from the data taken above
radius, distance_from_center = measure_curvature_radius_position(ploty, left_fitx, right_fitx)
# Specify the targeted image to be filled in with radius information
curvature_image = np.copy(combined)
# Fill the curvature information in targeted image
add_radius_distance(curvature_image, radius, distance_from_center)
# Show the images (before & after)
show2img(unwarped,curvature_image, "Unwarped image with boundaries", "Final image with curvature info")
# Save image with curvature information (for writeup)
mpimg.imsave('output_images/test1_curvature_info.jpg', curvature_image)
# Import everything needed to edit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML
# Show targeted videos in a line (for easy confirmation)
HTML("""<video width="320" height="180" title="Project video" controls>
<source src="project_video.mp4">
</video>
<video width="320" height="180" title="Challenge video" controls>
<source src="challenge_video.mp4">
</video>
<video width="320" height="180" title="Harder challenge video" controls>
<source src="harder_challenge_video.mp4">
</video>""")
# Pipepline to process each image
def process_image(img):
# Undistort the given image
undist = cv2.undistort(img, mtx, dist, None, mtx)
# Create binary image by applying thresholds
binary = thresh_binary(undist)
# Create a warped image by perspective transform
binary_warped, M_perspective = warped(binary, src, dst)
# Search method 1 & 2
if search_method == 1 or search_method == 2:
# Call the process to search & draw the lane lines
boundary, ploty, left_fitx, right_fitx = search_around_poly(binary_warped)
# Search method 3
else:
# Apply the search pipepline to binary image
boundary = convolution_search(binary_warped)
# Calculate the actual radius of curvature and distance from center in meters
radius, distance_from_center = measure_curvature_radius_position(ploty, left_fitx, right_fitx)
# Unwarped the binary image
# Apply perspective transform reversely by switching src & dst
binary_unwarped, M_perspective_reverse = warped(boundary, dst, src)
# Warp the detected lane boundaries back onto the original image
result = weighted_img(undist,binary_unwarped)
# Fill the curvature information in targeted image
add_radius_distance(result, radius, distance_from_center)
return result
# Generate output project video with search method 1 (for reference)
search_method = 1
# Initialize before processing the pipeline
lastFrame_init(lane_pickle, "lane_params.p")
# Apply the pipeline to process project video
# Firstly try with the first 10s
# project_video = VideoFileClip("project_video.mp4").subclip(0,10)
# Then try with full video
project_video = VideoFileClip("project_video.mp4")
project_video_output = 'output_videos/project_video_output_method1.mp4'
project_video_process = project_video.fl_image(process_image)
%time project_video_process.write_videofile(project_video_output, audio=False)
# Select the search method before processing the pipeline
search_method = 2
# Initialize before processing the pipeline
lastFrame_init(lane_pickle, "lane_params.p")
# Apply the pipeline to process project video
# Firstly try with the first 10s
# project_video = VideoFileClip("project_video.mp4").subclip(0,10)
# Then try with full video
project_video = VideoFileClip("project_video.mp4")
project_video_output = 'output_videos/project_video_output.mp4'
project_video_process = project_video.fl_image(process_image)
%time project_video_process.write_videofile(project_video_output, audio=False)
# Show 2 videos (with search method 1 & 2) in a line for easy comparison
HTML("""<video width="480" height="270" title="Output video - method 1" controls>
<source src="output_videos/project_video_output.mp4">
</video>
<video width="480" height="270" title="Output video - method 2" controls>
<source src="output_videos/project_video_output_method1.mp4">
</video>""")
# Initialize before processing the pipeline
lastFrame_init(lane_pickle, "lane_params.p")
# Apply the pipeline to challenge video
# Firstly try with the first 10s
# challenge_video = VideoFileClip("challenge_video.mp4").subclip(0,10)
# Then try with full video
challenge_video = VideoFileClip("challenge_video.mp4")
challenge_video_output = 'output_videos/challenge_video_output.mp4'
challenge_video_process = challenge_video.fl_image(process_image)
%time challenge_video_process.write_videofile(challenge_video_output, audio=False)
# Show 2 videos (before and after) in a line for easy comparison
HTML("""<video width="480" height="270" title="Challenge video" controls>
<source src="challenge_video.mp4">
</video>
<video width="480" height="270" title="Output video" controls>
<source src="output_videos/challenge_video_output.mp4">
</video>""")
# Initialize before processing the pipeline
lastFrame_init(lane_pickle, "lane_params.p")
# Apply the pipeline to challenge video
# Firstly try with the first 10s
# harder_challenge_video = VideoFileClip("harder_challenge_video.mp4").subclip(0,10)
# Then try with full video
harder_challenge_video = VideoFileClip("harder_challenge_video.mp4")
harder_challenge_video_output = 'output_videos/harder_challenge_video_output.mp4'
harder_challenge_video_process = harder_challenge_video.fl_image(process_image)
%time harder_challenge_video_process.write_videofile(harder_challenge_video_output, audio=False)
# Show 2 videos (before and after) in a line for easy comparison
HTML("""<video width="480" height="270" title="Harder challenge video" controls>
<source src="harder_challenge_video.mp4">
</video>
<video width="480" height="270" title="Output video" controls>
<source src="output_videos/harder_challenge_video_output.mp4">
</video>""")